package pt.ipleiria.plp.Lixeiras;

import pt.ipleiria.plp.*;
import java.util.Enumeration;
import java.util.Vector;
import pt.ipleiria.plp.stub.ArrayOfString;
import pt.ipleiria.plp.stub.BlackSpotState;
import pt.ipleiria.plp.stub.OperationStatus;
import pt.ipleiria.plp.stub.SpottingSoap_Stub;
import pt.ipleiria.plp.stub.WasteVolume;

/**
 * Class with all the data to be sent
 * @author Filipe Felisberto e Ricardo Silva
 */
public abstract class LixeiraPeddingComplete extends LixeiraPending {

    private ImagePath[] images;
    private boolean delImages = false;
    //FOR BLACKSPOT
    private double latitude;
    private double longitude;
    private String quantity;
    private String[] classification;
    private int estimatedNumberVolunteers;
    private String estimatedWasteVolume;
    private String notes;
    private String[] tags;
    private LixeiraDate inputDate;
    private LixeiraDate lastEditDate;
    private String status;

    public LixeiraPeddingComplete(String guid, String title, double latitude,
            double longitude, pt.ipleiria.plp.stub.WasteVolume quantity,
            java.lang.String[] classification, int estimatedNumberVolunteers,
            String estimatedWasteVolume, String notes,
            String[] tags, LixeiraDate inputDate,
            LixeiraDate lastEditDate, pt.ipleiria.plp.stub.BlackSpotState status) {

        super(guid, title, status.getValue());

        this.latitude = latitude;
        this.longitude = longitude;
        this.quantity = quantity.getValue();
        this.classification = classification;
        this.estimatedNumberVolunteers = estimatedNumberVolunteers;
        this.estimatedWasteVolume = estimatedWasteVolume;
        this.notes = notes;
        this.tags = tags;
        this.inputDate = inputDate;
        this.lastEditDate = lastEditDate;
        this.status = status.getValue();

    }

    public void edit(String title, double latitude,
            double longitude, pt.ipleiria.plp.stub.WasteVolume quantity,
            java.lang.String[] classification, int estimatedNumberVolunteers,
            String estimatedWasteVolume, String notes,
            String[] tags) {
        setTitle(title);
        this.latitude = latitude;
        this.longitude = longitude;
        this.quantity = quantity.getValue();
        this.classification = classification;
        this.estimatedNumberVolunteers = estimatedNumberVolunteers;
        this.estimatedWasteVolume = estimatedWasteVolume;
        this.notes = notes;
        this.tags = tags;
        this.lastEditDate = new LixeiraDate();

    }

    public String sendImages(String guid, String app, SpottingSoap_Stub service) {
        System.out.println("Start Sending images, number of im = " + getImages().length);
        Vector errors = new Vector();
        boolean noErrors = true;
        for (int i = 0; i < getImages().length; i++) {


            try {
                System.out.println("Stending: " + i);
                System.out.println("Stending Title: " + getImages()[i].getTitle());
                OperationStatus op = service.addMediaObject(guid, getImages()[i].getImageByte(),
                        getImages()[i].getTitle(), getImages()[i].getDescription(),
                        getImages()[i].getMediaType(), app);

                if (!op.isOperationSuceeded()) {
                    errors.addElement(op.getErrorMessage());
                    noErrors = false;
                }
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
                return "System Error!";
            }

        }
        if(noErrors){
            return null;
        }else{
            StringBuilder retErrors = new StringBuilder();
            Enumeration enums = errors.elements();
            int i = 0;
            while(enums.hasMoreElements()){


                retErrors.append((String)enums.nextElement());
                retErrors.append("\n");
            }
            System.out.println("ERRORS: "+ retErrors.toString());
            return retErrors.toString();
        }
    }

    /**
     * @return the latitude
     */
    public double getLatitude() {
        return latitude;
    }

    /**
     * @return the longitude
     */
    public double getLongitude() {
        return longitude;
    }

    /**
     * @return the quantity
     */
    public WasteVolume getQuantity() {

        return WasteVolume.fromValue(quantity);
    }

    /**
     * @return the classification
     */
    public java.lang.String[] getClassification() {
        return classification;
    }

    /**
     * @return the estimatedNumberVolunteers
     */
    public int getEstimatedNumberVolunteers() {
        return estimatedNumberVolunteers;
    }

    /**
     * @return the estimatedWasteVolume
     */
    public java.lang.String getEstimatedWasteVolume() {
        return estimatedWasteVolume;
    }

    /**
     * @return the notes
     */
    public java.lang.String getNotes() {
        return notes;
    }

    /**
     * @return the tags
     */
    public ArrayOfString getTags() {
        ArrayOfString array = new ArrayOfString(tags);
        return array;
    }

    /**
     * @return the inputDate
     */
    public LixeiraDate getInputDate() {
        return inputDate;
    }

    /**
     * @return the lastEditDate
     */
    public LixeiraDate getLastEditDate() {
        return lastEditDate;
    }

    /**
     * @return the status
     */
    public BlackSpotState getStatus() {

        return BlackSpotState.fromValue(status);
    }

    /**
     * @return the images
     */
    public ImagePath[] getImages() {
        return images;
    }

    /**
     * @param images the images to set
     */
    public void setImages(ImagePath[] images) {
        this.images = images;
    }

    /**
     * @return the images titles or null if non exist
     */
    public String[] getImagesTitles() {
        if (images.length == 0) {
            return null;
        }
        String[] titles = new String[images.length];
        for (int i = 0; i < images.length; i++) {
            titles[i] = images[i].getTitle();
        }
        return titles;
    }

    /**
     * @return the delImages
     */
    public boolean isDelImages() {
        return delImages;
    }

    /**
     * @param delImages the delImages to set
     */
    public void setDelImages(boolean delImages) {
        this.delImages = delImages;
    }
}
